package com.hitplane.manager 
{
	import com.hitplane.debug.DebugTrace;
	
	import flash.media.Sound;
	import flash.media.SoundMixer;

	/**
	 * 声音管理
	 * 单例模式类
	 * @author 郑子华
	 */
	public class SoundManager
	{
		private static var _instance:SoundManager = null;
		public function SoundManager(single:Single) 
		{
			if(single == null)
			{
				throw new Error("Can't create instance , Single is Null!");
			}
			init();
		}
		/**
		 * 单例引用
		 */
		public static function get instance():SoundManager
		{
			if(_instance == null)
			{
				_instance = new SoundManager(new Single());
			}
			return _instance;
		}
		//start===============================================================
		//普通子弹撞击的声音
		public const NORMAL_BULLET_HIT:String = "nomal_bullet_hit";
		
		public const NORMAL_BULLET_SEND:String = "normal_bullet_send";
		
		public const USER_FLY_BOMB:String = "user_fly_bomb";
		
		//游戏的背景音乐
		public const GAME_BG:String = "game_bg";
		
		//现在正在播放的声音，相同的声音只能有一个正在播放
		public var playingSound:Array = [];
		
		public var soundArr:Array = [];
		
		private function init():void {
			registerSound(NORMAL_BULLET_HIT, new NormalBulletHit_mp3);
			registerSound(NORMAL_BULLET_SEND, new NormalBulletSend_mp3);
			registerSound(USER_FLY_BOMB, new UserFlyBomb_mp3);
		}
		
		/**
		 * 注册声音
		 * @param	soundType
		 * @param	sound
		 */
		public function registerSound(soundType:String,sound:Sound):void {
			if (hasSound(soundType)) {
				DebugTrace.traceObj(this, "注册的声音已经存在");
				return ;
			}
			soundArr[soundType]=sound;
		}
		/**
		 * 移除声音
		 * @param	soundType
		 */
		public function moveSound(soundType:String):void {
			if (hasSound(soundType)) {
				soundArr[soundArr] = null;
			}else {
				DebugTrace.traceObj(this, "要移除的声音不存在");
			}
		}
		/**
		 * 是否有soundType指定的声音
		 * @param	soundType
		 * @return
		 */
		public function hasSound(soundType:String):Boolean {
			if(soundArr[soundType]==null){
				return false;
			}else{
				return true;
			}
		}
		/**
		 * 通过soundType获取声音
		 * @param	soundType
		 * @return
		 */
		public function getSoundByType(soundType:String):Sound {
			if (hasSound(soundType)) {
				return soundArr[soundType];
			}
			return null;
		}
		/**
		 * 播放soundType对应的声音
		 * @param	soundType
		 * @param	startTime	开始时间
		 * @param 	loop		循环次数
		 */
		public function playSoundByType(soundType:String,startTime:Number=0,loop:int=1):void {
			var sound:Sound = getSoundByType(soundType);
			if (sound != null){
//				sound.play(startTime,loop);
			} 
		}
		/**
		 * 停止所有播放的声音
		 */
		public function stopAllSound():void {
			SoundMixer.stopAll();
			playingSound = [];
		}
	}
}
class Single{}